package cn.lijigou.shopping.util;

import java.util.Vector;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.view.KeyEvent;

/**
 * @author ���=� E-mail:dong_yong_jian@163.com
 * @version CreatTime��2011-5-11 ����12:01:01
 * ��˵��
 */
public class TextUtil {
	private float mTextPosx = 0;// x���   
	private float mTextPosy = 0;// y���   
	private float mTextWidth = 0;// ���ƿ��   
	private float mTextHeight = 0;// ���Ƹ߶�   
	private int mFontHeight = 0;// ��������߶�   
	private int mPageLineNum = 0;// ÿһҳ��ʾ������   
	private int mCanvasBGColor = 0;// ������ɫ   
	private int mFontColor = 0;// ������ɫ   
	private int mAlpha = 0;// Alphaֵ   
	private int mRealLine = 0;// �ַ���ʵ������   
	private int mCurrentLine = 0;// ��ǰ��   
	private int mTextSize = 0;// �����С   
	private String mStrText = "";   
	private Vector mString = null;   
	private Paint mPaint = null;   
	  
	public float getmTextPosx() {
		return mTextPosx;
	}

	public void setmTextPosx(float mTextPosx) {
		this.mTextPosx = mTextPosx;
	}

	public float getmTextPosy() {
		return mTextPosy;
	}

	public void setmTextPosy(float mTextPosy) {
		this.mTextPosy = mTextPosy;
	}

	public float getmTextWidth() {
		return mTextWidth;
	}

	public void setmTextWidth(float mTextWidth) {
		this.mTextWidth = mTextWidth;
	}

	public float getmTextHeight() {
		return mTextHeight;
	}

	public void setmTextHeight(float mTextHeight) {
		this.mTextHeight = mTextHeight;
	}

	public int getmFontHeight() {
		return mFontHeight;
	}

	public void setmFontHeight(int mFontHeight) {
		this.mFontHeight = mFontHeight;
	}

	public int getmPageLineNum() {
		return mPageLineNum;
	}

	public void setmPageLineNum(int mPageLineNum) {
		this.mPageLineNum = mPageLineNum;
	}

	public int getmCanvasBGColor() {
		return mCanvasBGColor;
	}

	public void setmCanvasBGColor(int mCanvasBGColor) {
		this.mCanvasBGColor = mCanvasBGColor;
	}

	public int getmFontColor() {
		return mFontColor;
	}

	public void setmFontColor(int mFontColor) {
		this.mFontColor = mFontColor;
	}

	public int getmAlpha() {
		return mAlpha;
	}

	public void setmAlpha(int mAlpha) {
		this.mAlpha = mAlpha;
	}

	public int getmRealLine() {
		return mRealLine;
	}

	public void setmRealLine(int mRealLine) {
		this.mRealLine = mRealLine;
	}

	public int getmCurrentLine() {
		return mCurrentLine;
	}

	public void setmCurrentLine(int mCurrentLine) {
		this.mCurrentLine = mCurrentLine;
	}

	public int getmTextSize() {
		return mTextSize;
	}

	public void setmTextSize(int mTextSize) {
		this.mTextSize = mTextSize;
	}

	public String getmStrText() {
		return mStrText;
	}

	public void setmStrText(String mStrText) {
		this.mStrText = mStrText;
	}

	public Vector getmString() {
		return mString;
	}

	public void setmString(Vector mString) {
		this.mString = mString;
	}

	public Paint getmPaint() {
		return mPaint;
	}

	public void setmPaint(Paint mPaint) {
		this.mPaint = mPaint;
	}

	public TextUtil(String StrText, float x, float y, float w, float h, int bgcolor,   
	int textcolor, int alpha, int textsize) {   
	mPaint = new Paint();   
	mString = new Vector();   
	this.mStrText = StrText;   
	this.mTextPosx = x;   
	this.mTextPosy = y;   
	this.mTextWidth = w;   
	this.mTextHeight = h;   
	this.mCanvasBGColor = bgcolor;   
	this.mFontColor = textcolor;   
//	this.mAlpha = alpha;   
	this.mTextSize = textsize;   
	}   
	  
	public void InitText() {   
	mString.clear();// ���Vector   
	// �Ի������Ե�����   
	// mPaint.setARGB(this.mAlpha, Color.red(this.mFontColor), Color   
	// .green(this.mFontColor), Color.blue(this.mFontColor));   
	mPaint.setTextSize(this.mTextSize);   
	mPaint.setColor(mFontColor);   
	mPaint.setAntiAlias(true);   
	  
	this.GetTextIfon();   
	}   
	  
	/**  
	* �õ��ַ���Ϣ��(����ҳ�����Ϣ  
	*/  
	public void GetTextIfon() {   
		char ch;   
		int w = 0;   
		int istart = 0;   
		FontMetrics fm = mPaint.getFontMetrics();// �õ�ϵͳĬ����������   
		mFontHeight = (int) (Math.ceil(fm.descent - fm.top) + 2);// �������߶�   
		mPageLineNum = (int) (mTextHeight / mFontHeight);// �������   
		  
			int count = this.mStrText.length();   
			for (int i = 0; i < count; i++) {   
					ch = this.mStrText.charAt(i);   
					float[] widths = new float[1];   
					String str = String.valueOf(ch);   
					mPaint.getTextWidths(str, widths);   
						if (ch == '\n') {   
							mRealLine++;// ��ʵ�������һ   
							mString.addElement(this.mStrText.substring(istart, i));   
							istart = i + 1;   
							w = 0;   
						} else {   
						w += (int) Math.ceil(widths[0]);   
							if (w > this.mTextWidth) {   
							mRealLine++;// ��ʵ�������һ   
							mString.addElement(this.mStrText.substring(istart, i));   
							istart = i;   
							i--;   
							w = 0;   
						} else {   
							if (i == count - 1) {   
							mRealLine++;// ��ʵ�������һ   
							mString.addElement(this.mStrText.substring(istart,   
							count));   
						}   
					}   
				}   
			}   
	}   
	  
	/**  
	* �����ַ�  
	*   
	* @param canvas  
	*/  
	public void DrawText(Canvas canvas) {   
		for (int i = this.mCurrentLine, j = 0; i < this.mRealLine; i++, j++) {   
			if (j > this.mPageLineNum) {   
			break;   
			}   
			canvas.drawText((String) (mString.elementAt(i)), this.mTextPosx,   
			this.mTextPosy + this.mFontHeight * j, mPaint);  
			Paint p = new Paint();
			p.getFontMetrics();
			p.setColor(Color.BLACK);
    		p.setTextSize(12);
			canvas.drawText("4����ҳ", 50, 90, p);
		}   
	}   
	  
	/**  
	* ��ҳ�Ȱ�����  
	*   
	* @param keyCode  
	* @param event  
	* @return  
	*/  
	public boolean KeyDown(int keyCode, KeyEvent event) {   
	if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {   
	if (this.mCurrentLine > 0) {   
	this.mCurrentLine--;   
	}   
	} else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {   
	if ((this.mCurrentLine + this.mPageLineNum) < (this.mRealLine - 1)) {   
	this.mCurrentLine++;   
	}   
	}   
	return false;   
	}   

}
